package login;

import java.beans.*;
import java.awt.event.*;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.jsp.JspWriter;

public class SQLLoginBean implements Serializable
{
	//to be a JavaBean:
	//needs to be public
	//must have a public default constructor
	//must be serializable
	//must make properties available with get and set methods
	//may have events with methods that allow adding listeners
	
	//=========================properties===============================
	
	private String webuser;
	private String webpass;
	private String customerId;
	
	
	//=========================getters and setters=============================
	
	public String getUser() 
	{
		return webuser;
	}
	public void setUser(String user) 
	{
		this.webuser = user;
	}
	
	
	public String getPassword() 
	{
		return webpass;
	}
	public void setPassword(String password) 
	{
		this.webpass = password;
	}
	
	public String getCustomerId() 
	{
		return customerId;
	}
	public void setCustomerId(String custid) 
	{
		this.customerId = custid;
	}
	
	
	//=========================constructors======================
	
	public SQLLoginBean() 
	{
		setUser("");
		setPassword(""); 
	}
	
	public SQLLoginBean(String webuser, String webpass) 
	{
		setUser(webuser);
		setPassword(webpass); 
	}
	
	
	//=========================methods==================================
	

     // private method to get the customer id from 
	public String getBeanCustomerId(/*JspWriter out*/)
	{
		String customerId = "";
		try
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection loginConn =  DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orant11g", "ictoosd", "ictoosd");
			Statement stmnt = loginConn.createStatement();
			
			ResultSet resSet = stmnt.executeQuery("SELECT customerId, password FROM ictoosd.customerlogin WHERE username = '" + this.webuser + "'");
			//if there are results
			if (resSet.next()) 
		    {
		    	//test to make sure the database password matches the field password
				if (resSet.getString(2).equals(this.webpass))
				{
		    	 customerId = Integer.toString(resSet.getInt(1));
				}
		    }
		    else
		    {
		    	
		    }
		}
		catch (SQLException sqle) 
		{
			// TODO Auto-generated catch block
			sqle.printStackTrace();
		} 
		catch (ClassNotFoundException cnfe) 
		{
			// TODO Auto-generated catch block
			cnfe.printStackTrace();
		}
		
		return customerId;
	}
}
